package com.cip.master.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.velocity.app.VelocityEngine;
import javax.servlet.http.HttpServletRequest;

import com.cip.common.util.CommonUtil;
import com.cip.common.util.GridUtil;
import com.cip.master.dao.HolidayDAO;
import com.cip.master.model.Holiday;
import com.cip.master.service.HolidayService;

public class HolidayServiceImpl implements HolidayService {

	private HolidayDAO holidayDAO;
	private VelocityEngine velocityEngine;
	
	public String getHolidayGridList(Locale locale,HttpServletRequest request)throws Exception {
		
		String gridSource = ""; 
		
		try {
			List<Holiday> holidayList = getAllHolidayList();
			
			List<Map<String,Object>> holidayArrayList = new ArrayList<Map<String,Object>>();
			
			for (Holiday holiday : holidayList) {
				Map<String,Object> holidayObj = new HashMap<String, Object>();
				if(request.getSession().getAttribute("role")== "ROLE_SUPER_ADMIN" || request.getSession().getAttribute("role")== "ROLE_ADMIN") {
					
					holidayObj.put("name", "<a href='/editHoliday?method=showEditHoliday&holiday.id="+holiday.getId()+"' style='text-decoration:underline;color:blue;'>"+holiday.getHolidayName()+"</a>");
					holidayObj.put("status", holiday.getStatus());
				}else{
					holidayObj.put("name", holiday.getHolidayName());
				}
				
				holidayObj.put("fromdate", holiday.getFromDate());
				holidayObj.put("todate", holiday.getToDate());
				holidayObj.put("description", holiday.getDescription());
				
				holidayArrayList.add(holidayObj);			
			}
			
			gridSource = generateScriptForProcess(velocityEngine, holidayArrayList, locale,request);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return gridSource;
	}
	
	/**
	 * Set into context of grid column names and field names and it attributes
	 * 
	 * @param context
	 * @return
	 * @throws Exception
	 */
	public static String generateScriptForProcess(VelocityEngine velocityEngine, List<Map<String, Object>> holidayArrayList, Locale locale,HttpServletRequest request) throws Exception {
		
		return GridUtil.generateScriptForProcess(velocityEngine, holidayArrayList, createHolidayGridColumnMapping(locale,request));
	}
	
	/**
	 * Add needed column and it mapping for my instances
	 * 
	 * @param context
	 */
	private static Map<String, Object> createHolidayGridColumnMapping(Locale locale,HttpServletRequest request) {
		Map<String, Object> context = new HashMap<String, Object>();
		List<Map<String, Object>> fieldNameList = new ArrayList<Map<String, Object>>();
		//ResourceBundle appResourceBundle = ResourceBundle.getBundle(Constants.BUNDLE_KEY ,locale);
		//context.put("title", appResourceBundle.getString("process.myInstances"));
		context.put("title", "Holiday List");
		context.put("gridId", "MY_HOLIDAYLIST");
		context.put("needTreeStructure", true);
		//context.put("dynamicGridWidth", "300px");
		//context.put("dynamicGridHeight", "580px");
		String columnNames = "";
		if(request.getSession().getAttribute("role")== "ROLE_SUPER_ADMIN" || request.getSession().getAttribute("role")== "ROLE_ADMIN") {
			columnNames = "['Holiday Name','From Date','To Date','Description','Status']";
		}else{
			columnNames = "['Holiday Name','From Date','To Date','Description']";
		}
		
        CommonUtil.createFieldNameList(fieldNameList, "name", "100", "center", "","false");
        CommonUtil.createFieldNameList(fieldNameList, "fromdate", "100", "left","", "false");
        CommonUtil.createFieldNameList(fieldNameList, "todate", "100", "left","", "false");
        CommonUtil.createFieldNameList(fieldNameList, "description", "100", "left","", "false");
        if(request.getSession().getAttribute("role")== "ROLE_SUPER_ADMIN" || request.getSession().getAttribute("role")== "ROLE_ADMIN") {
        	CommonUtil.createFieldNameList(fieldNameList, "status", "100", "center", "", "false");
        }

        context.put("columnNames", columnNames);
        context.put("noOfRecords", "10");
		context.put("fieldNameList", fieldNameList);
		context.put("gridHeading", "true");
		
		return context;
	}

	public List<Holiday> getAllHolidayList() throws Exception {
		return holidayDAO.getAllHolidayList();
	}

	public Holiday getHolidayById(int id) throws Exception {
		return holidayDAO.getHolidayById(id);
	}

	public Holiday saveOrUpdateHoliday(Holiday holiday) throws Exception {		
		return holidayDAO.saveOrUpdateHoliday(holiday);
	}

	public Holiday editHoliday(Holiday holiday) throws Exception {
		return holidayDAO.editHoliday(holiday);
	}
	
	public HolidayDAO getHolidayDAO() {
		return holidayDAO;
	}

	public void setHolidayDAO(HolidayDAO holidayDAO) {
		this.holidayDAO = holidayDAO;
	}

	public VelocityEngine getVelocityEngine() {
		return velocityEngine;
	}

	public void setVelocityEngine(VelocityEngine velocityEngine) {
		this.velocityEngine = velocityEngine;
	}
	
}
